﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Jayden.Dll.Navigation.Core
{
	public class Coordinate : CorePathNode
	{
		private double m_Latitude;
		private double m_Longitude;

		public static double ToRadians(double angle)
		{
			return Math.PI * angle / 180.0;
		}
		public static double Distance(Coordinate c1, Coordinate c2)
		{
			//double dx = c1.Latitude - c2.Latitude;
			//double dy = c1.Longitude - c2.Longitude;
			//return Math.Sqrt(dx * dx + dy * dy);

			double r = 6371;
			double  distance = Math.Acos(
				Math.Sin(ToRadians(c1.Latitude)) * Math.Sin(ToRadians(c2.Latitude)) +
				Math.Cos(ToRadians(c1.Latitude)) * Math.Cos(ToRadians(c2.Latitude)) *
				Math.Cos(ToRadians(c2.Longitude - c1.Longitude))) * r;
			return distance;
		}

		public Coordinate(double latitude, double longitude)
		{
			m_Latitude = latitude;
			m_Longitude = longitude;
		}

		public double Latitude { get { return m_Latitude; } }
		public double Longitude { get { return m_Longitude; } }

		public override string ToString() { return string.Format("{0}, {1}", m_Latitude.ToString(CultureInfo.InvariantCulture), m_Longitude.ToString(CultureInfo.InvariantCulture)); }
		public override int GetHashCode() { return m_Latitude.GetHashCode() * 31 + m_Longitude.GetHashCode(); }
		public override bool Equals(object obj)
		{
			if (obj == this)
				return true;
			if (obj is Coordinate)
			{
				Coordinate other = obj as Coordinate;
				return m_Latitude.Equals(other.m_Latitude) && m_Longitude.Equals(other.m_Longitude);
			}
			return false;
		}
	}
}
